package com.example.forum.controller;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.MessageService;
import com.example.forum.services.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    //注入业务层
    @Resource
    MessageService messageService;

    /**
     * 发送私信
     *
     * @param request
     * @param receiveUserId
     * @param content
     * @return
     */
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request, Long receiveUserId, String content) {
        Message message = new Message();
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //构建对象
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        message.setPostUserId(user.getId());
        messageService.create(message);
        return AppResult.success();
    }

    /**
     * 未读的数量
     *
     * @param request
     * @return
     */
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request) {
        //根据UserID去查询
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        int ret = messageService.selectUnreadCount(user.getId());
        return AppResult.success(ret);
    }


    /**
     * 构建私信列表
     *
     * @param request
     * @return
     */
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {

        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //我自己就是接收者呀！
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }


    /**
     * 查看私信
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping("/markRead")
    public AppResult<Message> markRead(HttpServletRequest request, Long id) {
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //查询到回复信息
        Message message = messageService.selectById(id);
        //跟新状态
        messageService.updateStateById(id, (byte) 1);
        return AppResult.success(message);
    }

    /**
     * 回复私信
     *
     * @param request
     * @param repliedId
     * @param content
     * @return
     */
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request, Long repliedId, String content) {
        //初始化信息
        Message message = new Message();
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //构建对象
        message.setContent(content);
        message.setPostUserId(user.getId());
        //依然是添加一条站内信
        messageService.reply(repliedId, message);
        return AppResult.success();
    }

}
